package com.stenio.fancat.controller.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.shop.FcEtOrderLog;
import com.stenio.fancat.service.shop.EtOrderLogService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuan
 * @create 2019-03-02 13:39
 */
@RestController
@RequestMapping("etLog")
public class EtOrderLogController {

    @Autowired
    private EtOrderLogService etOrderLogService;

    @PostMapping("findEtLog/{id}/{babyId}")
    public Result findEtLog(@PathVariable String id,@PathVariable String babyId) {
        Result result = new Result();
        List<FcEtOrderLog> fcEtOrderLogs = etOrderLogService.selectList(new EntityWrapper<FcEtOrderLog>()
                .eq(FcEtOrderLog.BABY_ID,babyId)
                .eq(FcEtOrderLog.USER_ID, id));
        Map<String,List<FcEtOrderLog>> etOrderLogs = new HashMap<>();
        SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");
        if(CollectionUtils.isNotEmpty(fcEtOrderLogs)) {
            for (FcEtOrderLog fcEtOrderLog : fcEtOrderLogs) {
                String format = s.format(fcEtOrderLog.getCreateTime());
                if(etOrderLogs.containsKey(format)) {
                    etOrderLogs.get(format).add(fcEtOrderLog);
                }else {
                    List<FcEtOrderLog> newList = new ArrayList<>();
                    newList.add(fcEtOrderLog);
                    etOrderLogs.put(format,newList);
                }
            }
        }else {
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        List etOrderLogList = new ArrayList();
        for(Map.Entry entry : etOrderLogs.entrySet()) {
            Map newMap = new HashMap();
            newMap.put("dateTime",entry.getKey());
            newMap.put("order",entry.getValue());
            etOrderLogList.add(newMap);
        }
        result.setData(etOrderLogList);
        return result;
    }

}
